<?php
namespace Admin\Model;
use Think\Model;
class ProjectModel extends Model 
{
	protected $insertFields = array('username','admin_id','project_name','shuju','company','jingdu','weidu','contactname','mobile');
	protected $updateFields = array('id','username','admin_id','project_name','shuju','company','jingdu','weidu','contactname','mobile');
	protected $_validate = array(
		
		
		array('project_name', 'require', '项目名称不能为空！', 1, 'regex', 3),
		array('project_name', '1,200', '项目名称的值最长不能超过 200 个字符！', 1, 'length', 3),
		
		array('company', 'require', '甲方单位不能为空！', 1, 'regex', 3),
		array('company', '1,200', '甲方单位的值最长不能超过 200 个字符！', 1, 'length', 3),
		array('jingdu', 'require', '经度不能为空！', 1, 'regex', 3),
		array('jingdu', '1,100', '经度的值最长不能超过 100 个字符！', 1, 'length', 3),
		array('weidu', 'require', '纬度不能为空！', 1, 'regex', 3),
		array('weidu', '1,100', '纬度的值最长不能超过 100 个字符！', 1, 'length', 3),
		array('contactname', 'require', '联系人不能为空！', 1, 'regex', 3),
		array('contactname', '1,100', '联系人的值最长不能超过 100 个字符！', 1, 'length', 3),
		array('mobile', 'require', '手机号不能为空！', 1, 'regex', 3),
		array('mobile', '1,100', '手机号的值最长不能超过 100 个字符！', 1, 'length', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($username = I('get.username'))
			$where['username'] = array('like', "%$username%");
		if($admin_id = I('get.admin_id'))
			$where['admin_id'] = array('like', "%$admin_id%");
		if($project_name = I('get.project_name'))
			$where['project_name'] = array('like', "%$project_name%");
		if($shuju = I('get.shuju'))
			$where['shuju'] = array('like', "%$shuju%");
		if($company = I('get.company'))
			$where['company'] = array('like', "%$company%");
		if($jingdu = I('get.jingdu'))
			$where['jingdu'] = array('like', "%$jingdu%");
		if($weidu = I('get.weidu'))
			$where['weidu'] = array('like', "%$weidu%");
		if($contactname = I('get.contactname'))
			$where['contactname'] = array('like', "%$contactname%");
		if($mobile = I('get.mobile'))
			$where['mobile'] = array('like', "%$mobile%");
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		$data['addtime'] = date('Y-m-d H:i:s');
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		$data['addtime'] = date('Y-m-d H:i:s');
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}